package org.paysim.payment.controller;

import java.util.HashMap;
import java.util.Map;

import org.paysim.constant.ResponseMessage;
import org.paysim.payment.model.PaymentCancelRequest;
import org.paysim.payment.model.PaymentCancelResponse;
import org.paysim.payment.model.PaymentCancelResponseData;
import org.paysim.payment.model.PaymentInitiateRequest;
import org.paysim.payment.model.PaymentInitiateResponse;
import org.paysim.payment.model.PaymentQueryResponse;
import org.paysim.payment.service.IPaymentService;
import org.paysim.payment.service.PaymentStrategyFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;

@RestController
@RequestMapping("${openapi.paySim.base-path:}")
public class PaymentApiController implements PaymentApi {
    @Resource
    private PaymentStrategyFactory paymentStrategy;
    
    @Qualifier("AlipayService")
    private IPaymentService paymentService;

    @Override
    public ResponseEntity<PaymentCancelResponse> paymentCancelPost(@Valid PaymentCancelRequest paymentCancelRequest) {
        // TODO Auto-generated method stub
        String method = paymentCancelRequest.getPaymentMethod();
        IPaymentService paymentService = paymentStrategy.getStrategy(method);
        PaymentCancelResponseData paymentCancel = paymentService.cancelPayment(paymentCancelRequest);
        PaymentCancelResponse response = new PaymentCancelResponse().code(ResponseMessage.SUCCESS.getCode())
                .message(ResponseMessage.SUCCESS.getMessage()).data(paymentCancel);
        return ResponseEntity.ok(response);
    }

    @Override
    public ResponseEntity<PaymentInitiateResponse> paymentInitiatePost(
            @Valid PaymentInitiateRequest paymentInitiateRequest) {
        String method = paymentInitiateRequest.getPaymentMethod();
        IPaymentService paymentService = paymentStrategy.getStrategy(method);
        String form = paymentService.initiatePayment(paymentInitiateRequest);
        PaymentInitiateResponse paymentInitiateResponse = new PaymentInitiateResponse()
                .code(ResponseMessage.SUCCESS.getCode()).message(ResponseMessage.SUCCESS.getMessage()).data(form);
        return ResponseEntity.ok(paymentInitiateResponse);
    }

    @Override
    public ResponseEntity<PaymentQueryResponse> paymentQueryGet(@NotNull @Valid String orderId, @Valid Object body) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public ResponseEntity<String> paymentNotifyPost(HttpServletRequest request) {
        if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
            Map<String, String> params = new HashMap<>();
            Map<String, String[]> requestParams = request.getParameterMap();
            for (String name : requestParams.keySet()) {
                params.put(name, request.getParameter(name));
            }
            // 支付宝验签
            paymentService.notifyPayment(params);
            return ResponseEntity.ok("success");
        } else {
            return ResponseEntity.ok("trade_status is not TRADE_SUCCESS");
        }

    }
}
